﻿namespace EyeOpen.DataAnnotations
{
	using System.Collections.Generic;
	using System.Web.Mvc;

	public class CsvDataAnnotationsModelValidator :
		DataAnnotationsModelValidator<CsvAttribute>
	{
		private readonly string errorMessage;

		public CsvDataAnnotationsModelValidator(
			ModelMetadata metadata, ControllerContext context, CsvAttribute attribute)
			: base(metadata, context, attribute)
		{
			errorMessage = attribute.FormatErrorMessage(metadata.DisplayName);
		}

		public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
		{
			var rule =
				new ModelClientValidationRule
					{
						ErrorMessage = errorMessage,
						ValidationType = "csv"
					};

			rule.ValidationParameters.Add("minimum", Attribute.Minimum);
			
			yield return rule;
		}
	}
}